<?php

class Videos {

    private $_ID;
    private $_title;
    private $_image;
    private $_filePath;
    private $_createdDate;
    private $_modifiedDate;
    private $_createdUserID;
    private $_modifiedUserID;
    private $_status;
    private $_order;
    private $_fromYouTube;
    private $_width;
    private $_height;
    private $_createdUser;
    private $_modifiedUser;

    public function getCreatedUser() {
        return $this->_createdUser;
    }

    public function getModifiedUser() {
        return $this->_modifiedUser;
    }

    public function setCreatedUser($CreatedUser) {
        $this->_createdUser = $CreatedUser;
    }

    public function setModifiedUser($ModifiedUser) {
        $this->_modifiedUser = $ModifiedUser;
    }

    public function getWidth() {
        return $this->_width;
    }

    public function getHeight() {
        return $this->_height;
    }

    public function setWidth($Width) {
        $this->_width = $Width;
    }

    public function setHeight($Height) {
        $this->_height = $Height;
    }

    public function getID() {
        return $this->_ID;
    }

    public function getTitle() {
        return $this->_title;
    }

    public function getImage() {
        return $this->_image;
    }

    public function getFilePath() {
        return $this->_filePath;
    }

    public function getCreatedDate() {
        return $this->_createdDate;
    }

    public function getModifiedDate() {
        return $this->_modifiedDate;
    }

    public function getCreatedUserID() {
        return $this->_createdUserID;
    }

    public function getModifiedUserID() {
        return $this->_modifiedUserID;
    }

    public function getStatus() {
        return $this->_status;
    }

    public function getOrder() {
        return $this->_order;
    }

    public function getFromYouTube() {
        return $this->_fromYouTube;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function setTitle($Title) {
        $this->_title = $Title;
    }

    public function setImage($Image) {
        $this->_image = $Image;
    }

    public function setFilePath($FilePath) {
        $this->_filePath = $FilePath;
    }

    public function setCreatedDate($CreatedDate) {
        $this->_createdDate = $CreatedDate;
    }

    public function setModifiedDate($ModifiedDate) {
        $this->_modifiedDate = $ModifiedDate;
    }

    public function setCreatedUserID($CreatedUserID) {
        $this->_createdUserID = $CreatedUserID;
    }

    public function setModifiedUserID($ModifiedUserID) {
        $this->_modifiedUserID = $ModifiedUserID;
    }

    public function setStatus($Status) {
        $this->_status = $Status;
    }

    public function setOrder($Order) {
        $this->_order = $Order;
    }

    public function setFromYouTube($FromYouTube) {
        $this->_fromYouTube = $FromYouTube;
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `videos`
                                (
                                 `V_Title`,
                                 `V_Image`,
                                 `V_FilePath`,
                                 `V_CreatedDate`,
                                 `V_ModifiedDate`,
                                 `V_Status`,
                                 `V_Order`,
                                 `V_FromYouTube`,
                                 `V_CreatedUserID`,
                                 `V_ModifiedUserID`,
                                 `V_Width`,
                                 `V_Height`)
                    VALUES (
                            :V_Title,
                            :V_Image,
                            :V_FilePath,
                            :V_CreatedDate,
                            :V_ModifiedDate,
                            :V_Status,
                            :V_Order,
                            :V_FromYouTube,
                            :V_CreatedUserID,
                            :V_ModifiedUserID,
                            :V_Width,
                            :V_Height);";

            $parameters = array(
                ':V_Title', ':V_Image',
                ':V_FilePath', ':V_CreatedDate',
                ':V_ModifiedDate', ':V_Status',
                ':V_Order', ':V_FromYouTube',
                ':V_CreatedUserID', ':V_ModifiedUserID',
                ':V_Width', ':V_Height'
            );

            $values = array(
                $this->getTitle(), $this->getImage(),
                $this->getFilePath(), $this->getCreatedDate(),
                $this->getModifiedDate(), $this->getStatus(),
                $this->getOrder(), $this->getFromYouTube(),
                $this->getCreatedUserID(), $this->getModifiedUserID(),
                $this->getWidth(), $this->getHeight()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_INT
            );

            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function update($db = null) {

        if ($db != null) {
            $sql = "UPDATE `videos`
                    SET
                      `V_Title` = :V_Title,
                      `V_Image` = :V_Image,
                      `V_FilePath` = :V_FilePath,
                      `V_ModifiedDate` = :V_ModifiedDate,
                      `V_Status` = :V_Status,
                      `V_FromYouTube` = :V_FromYouTube,
                      `V_Width` = :V_Width,
                      `V_Height` = :V_Height,
                      `V_ModifiedUserID` = :V_ModifiedUserID
                    WHERE `V_ID` = :V_ID;";

            $parameters = array(
                ':V_Title', ':V_Image',
                ':V_FilePath',
                ':V_ModifiedDate', ':V_Status',
                ':V_FromYouTube',
                ':V_Width',
                ':V_Height',
                ':V_ModifiedUserID',
                ':V_ID'
            );

            $values = array(
                $this->getTitle(), $this->getImage(),
                $this->getFilePath(),
                $this->getModifiedDate(), $this->getStatus(),
                $this->getFromYouTube(),
                $this->getWidth(),
                $this->getHeight(),
                $this->getModifiedUserID(),
                $this->getID()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_INT
            );

            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function delete($db = null) {

        if ($db != null) {
            $sql = "DELETE
                    FROM `videos`
                    WHERE `V_ID` = :V_ID;";

            $parameters = array(':V_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function getList($db) {
        if ($db != null) {

            $sql = 'SELECT
                    `V_ID`,
                    `V_Title`,
                    `V_Image`,
                    `V_FilePath`,
                    `V_CreatedDate`,
                    `V_ModifiedDate`,
                    `V_Status`,
                    `V_Order`,
                    `V_FromYouTube`,
                    `V_CreatedUserID`,
                    `V_ModifiedUserID`,
                     V_Width,
                     V_Height
                  FROM `videos`
                  ORDER BY V_Order DESC';

            $parameters = null;
            $values = null;
            $types = null;

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $video = new Videos();
                    $video->setID($row->V_ID);
                    $video->setTitle($row->V_Title);
                    $video->setImage($row->V_Image);
                    $video->setFilePath($row->V_FilePath);
                    $video->setCreatedDate($row->V_CreatedDate);
                    $video->setModifiedDate($row->V_ModifiedDate);
                    $video->setStatus($row->V_Status);
                    $video->setOrder($row->V_Order);
                    $video->setFromYouTube($row->V_FromYouTube);
                    $video->setWidth($row->V_Width);
                    $video->setHeight($row->V_Height);

                    $video->setCreatedUserID($row->V_CreatedUserID);
                    $video->setModifiedUserID($row->V_ModifiedUserID);


                    // created user
                    $createdUser = new Users();
                    $createdUser->setID($video->getCreatedUserID());
                    $createdUser->getByID($db);
                    $video->setCreatedUser($createdUser);


                    // modified user
                    $modifiedUser = new Users();
                    $modifiedUser->setID($video->getModifiedUserID());
                    $modifiedUser->getByID($db);
                    $video->setModifiedUser($modifiedUser);


                    $rows[] = $video;
                }
                return $rows;
            } else {
                return null;
            }
        } else {
            echo 'Connection is null';
        }
    }

    public function getByID($db = null) {

        if ($db != null) {

            $sql = 'select
                        `V_ID`,
                        `V_Title`,
                        `V_Image`,
                        `V_FilePath`,
                        `V_CreatedDate`,
                        `V_ModifiedDate`,
                        `V_Status`,
                        `V_Order`,
                        `V_FromYouTube`,
                        `V_CreatedUserID`,
                        `V_ModifiedUserID`,
                        `V_Width`,
                        `V_Height`
                      from `videos`
                      where `V_ID` = :V_ID;';

            $parameters = array(':V_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {
                $row = $result->fetch();
                $this->setID($row->V_ID);
                $this->setTitle($row->V_Title);
                $this->setImage($row->V_Image);
                $this->setFilePath($row->V_FilePath);
                $this->setCreatedDate($row->V_CreatedDate);
                $this->setModifiedDate($row->V_ModifiedDate);
                $this->setStatus($row->V_Status);
                $this->setOrder($row->V_Order);
                $this->setFromYouTube($row->V_FromYouTube);
                $this->setCreatedUserID($row->V_CreatedUserID);
                $this->setModifiedUserID($row->V_ModifiedUserID);
                $this->setWidth($row->V_Width);
                $this->setHeight($row->V_Height);

                return $this;
            } else {
                return null;
            }
        }
    }

    public function getMaxOrder($db) {
        if ($db != null) {
            $sql = "SELECT MAX(V_Order) + 1 AS MaxOrder FROM videos";
            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            $row = $result->fetch();
            if ($row->MaxOrder != null) {
                return $row->MaxOrder;
            } else {
                return 1;
            }
        }
    }

    public function orderUp($db) {
        if ($db != null) {
            // câu lệnh tải currentOrder, nextOrder, nextArticleID
            $sql = "SELECT (SELECT `V_Order` 
                    from videos 
                    WHERE V_ID = :V_ID) As `currenOrder`,               
                
                    (SELECT `V_Order` from videos WHERE `V_Order` > 
                    (SELECT `V_Order` from videos WHERE V_ID = :V_ID) 
                    Order by `V_Order` LIMIT 1) As `nextOrder`,

                    (SELECT V_ID from videos WHERE `V_Order` = 
                    (SELECT `V_Order` from videos WHERE `V_Order` > 
                    (SELECT `V_Order` from videos WHERE V_ID = :V_ID) 
                    Order by `V_Order` LIMIT 1)) As `nextArticleID`";
            $parameters = array(':V_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sql, $parameters, $values, $types);



            if ($result != null) {
                $row = $result->fetch();
                // cập nhật order của previous article bằng với currentOrder
                $sqlNextArticle = "update videos set `V_Order` = :V_Order where `V_ID` = :V_ID";
                $parameters2 = array(':V_Order', ':V_ID');
                $values2 = array($row->currenOrder, $row->nextArticleID);
                $types2 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                $db->executeNonQuery($sqlNextArticle, $parameters2, $values2, $types2);

                // cập nhật order của current article bằng với previous order
                $sqlCurrentArticle = "update videos set `V_Order` = :V_Order where `V_ID` = :V_ID";
                $parameters3 = array(':V_Order', ':V_ID');
                $values3 = array($row->nextOrder, $this->getID());
                $types3 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                $db->executeNonQuery($sqlCurrentArticle, $parameters3, $values3, $types3);
            }
        }
    }

    function orderDown($db) {
        if ($db != null) {
            // câu lệnh tải currentOrder, previousOrder, previousArticleID
            $sql = "SELECT (SELECT `V_Order` from videos 
                WHERE V_ID = :V_ID) As `currenOrder`, 
                    
		(SELECT `V_Order` from videos WHERE `V_Order` < 
		(SELECT `V_Order` from videos WHERE V_ID = :V_ID) 
		Order by `V_Order` desc LIMIT 1) As `previousOrder`,
                
		(SELECT V_ID from videos WHERE `V_Order` = 
		(SELECT `V_Order` from videos WHERE `V_Order` < 
		(SELECT `V_Order` from videos WHERE V_ID = :V_ID) 
		Order by `V_Order` desc LIMIT 1)) As `previousArticleID`";

            $parameters = array(':V_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            if ($result != null) {

                $row = $result->fetch();

                // cập nhật order của previous bằng với currentOrder
                $sqlPreviousArticle = "update videos set `V_Order` = :V_Order where `V_ID` = :V_ID";
                $parameters2 = array(':V_Order', ':V_ID');
                $values2 = array($row->currenOrder, $row->previousArticleID);
                $types2 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                $db->executeNonQuery($sqlPreviousArticle, $parameters2, $values2, $types2);

                // cập nhật order của current article bằng với previous order
                $sqlCurrentArticle = "update videos set `V_Order` = :V_Order where `V_ID` = :V_ID";
                $parameters3 = array(':V_Order', ':V_ID');
                $values3 = array($row->previousOrder, $this->getID());
                $types3 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                $db->executeNonQuery($sqlCurrentArticle, $parameters3, $values3, $types3);
            }
        }
    }

}
